package com.example.demo.algorithm.graph;

/**
 * @Author: xiatian
 * @CreateTime: 2022/2/13 4:21 下午
 * @Description: 加权无向图_加权边的表示
 */
public class Edge implements Comparable<Edge> {
    //边的一个顶点v
    private final int v;
    //边的另一个顶点w
    private final int w;
    private final double weight;

    /**
     * 通过顶点v和w，以及权重weight值构造一个边对象
     *
     * @param v
     * @param w
     * @param weight
     */
    public Edge(int v, int w, double weight) {
        this.v = v;
        this.w = w;
        this.weight = weight;
    }

    /**
     * 获取边的权重值
     *
     * @return
     */
    public double weight() {
        return weight;
    }

    /**
     * 获取边上的一个点
     *
     * @return
     */
    public int either() {
        return v;
    }

    /**
     * 获取边上除了顶点vertex外的另一个顶点
     *
     * @param vertex
     * @return
     */
    public int other(int vertex) {
        if (vertex == v) {
            return w;
        } else {
            return v;
        }
    }

    /**
     * 比较当前边和传过来的边的权重值大小
     *
     * @param that
     * @return
     */
    @Override
    public int compareTo(Edge that) {
        int cmp;
        if (this.weight() > that.weight()) {
            cmp = 1;
        } else if (this.weight() < that.weight()) {
            cmp = -1;
        } else {
            cmp = 0;
        }
        return cmp;
    }
}
